package com.bitmart.biometric;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.biometric.BiometricPrompt;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.biometric.core.authenticator.AppAuthenticator;
import com.blankj.utilcode.util.ToastUtils;
import com.wangnan.library.GestureLockView;
import com.wangnan.library.listener.OnGestureLockListener;

public class GestureLockViewAuthenticatorFragment extends DialogFragment implements AppAuthenticator, OnGestureLockListener {
    private FragmentActivity host;
    private AuthenticateListener authenticateListener;

    GestureLockViewAuthenticatorFragment(FragmentActivity activity) {
        this.host = activity;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.custome_authenticator_ui_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        GestureLockView gestureLockView = view.findViewById(R.id.gestureLockView);
        gestureLockView.setGestureLockListener(this);
    }

    @Override
    public void authenticate(@NonNull AuthenticateListener authenticateListener) {
        this.authenticateListener = authenticateListener;
        show(host.getSupportFragmentManager(), "GestureLockFragment");
    }

    @Override
    public void reAuthenticate() {
        //do nothing
    }


    @Override
    public void cancelAuthenticate() {
        dismissAllowingStateLoss();
    }

    @Override
    public void onStarted() {

    }

    @Override
    public void onProgress(String progress) {

    }

    @Override
    public void onComplete(String result) {
        ToastUtils.showShort(result);
        if (result.equals("012345")) {
            authenticateListener.onSuccess();

        } else {
            // 如果图案没有滑完，还是类似指纹无效的回调一样，当做 fail 处理，而不是 error
            authenticateListener.onFailed();
        }
        dismiss();
    }
}
